﻿Imports System.Windows.Controls
Imports PTIC_Util
Imports OMANE.Common.Model.Interfaces

Namespace Views
    Public Class ViewEncoursRetrait

        Sub New()

            ' Cet appel est requis par le concepteur.
            InitializeComponent()

            AddHandler CType(Me.DataContext, ViewModels.ViewModelCmdEncoursRetrait).SelectedTrancheAgeChanged, AddressOf SelectedTrancheAgeChanged
            AddHandler CType(Me.DataContext, ViewModels.ViewModelCmdEncoursRetrait).SelectedTrancheCapiChanged, AddressOf SelectedTrancheCapiChanged

            ' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
            DataGridTrAge.IsReadOnly = DatagridAgeIsReadonly
        End Sub

        Private Shared DatagridAgeIsReadonly As Boolean = True

        Private Sub RefreshTrancheAgeIsReadOnly(ByVal newNomFichierTrAge As String)
            'Vérification pour voir s'il faut activer le mode lecture seule ou non
            If newNomFichierTrAge = "trAgeTemp.sas7bdat" Then
                If Not DataGridTrAge Is Nothing Then DataGridTrAge.IsReadOnly = False
            Else
                If Not DataGridTrAge Is Nothing Then DataGridTrAge.IsReadOnly = True
            End If
            If DataGridTrAge Is Nothing Then Return
            DatagridAgeIsReadonly = DataGridTrAge.IsReadOnly
        End Sub
        Private Sub RefreshTrancheCapiIsReadOnly(ByVal newNomFichierTrCapi As String)
            If newNomFichierTrCapi = "trCapiTemp.sas7bdat" Then
                If Not XamDataGridTrCapitaux Is Nothing Then XamDataGridTrCapitaux.IsReadOnly = False
            Else
                If Not XamDataGridTrCapitaux Is Nothing Then XamDataGridTrCapitaux.IsReadOnly = True
            End If
        End Sub

        Private Sub SelectedTrancheAgeChanged(ByVal sender As Object, ByVal e As PropertyChangeEventArgs(Of IInfoTableReferentiel))
            RefreshTrancheAgeIsReadOnly(e.NewValue.NomFichier)
        End Sub
        Private Sub SelectedTrancheCapiChanged(ByVal sender As Object, ByVal e As PropertyChangeEventArgs(Of IInfoTableReferentiel))
            RefreshTrancheCapiIsReadOnly(e.NewValue.NomFichier)
        End Sub

        Private Sub DataGridTrAge_AutoGeneratedColumns(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridTrAge.AutoGeneratedColumns, XamDataGridTrCapitaux.AutoGeneratedColumns
            Dim datagrd As DataGrid = sender

            datagrd.Columns(0).IsReadOnly = False
            datagrd.Columns(0).CanUserReorder = False
            datagrd.Columns(0).CanUserSort = False
            datagrd.Columns(1).IsReadOnly = True
            datagrd.Columns(1).Visibility = Windows.Visibility.Collapsed
            datagrd.Columns(1).CanUserReorder = False
            datagrd.Columns(1).CanUserSort = False

            If datagrd Is XamDataGridTrCapitaux Then
                Dim c = datagrd.Columns.Cast(Of DataGridBoundColumn).Last()

                c.Binding = New Windows.Data.Binding(
                    CType(c.Binding, Windows.Data.Binding).Path.Path) With {.StringFormat = "N0"}
            End If
            If sender Is DataGridTrAge Then RefreshTrancheAgeIsReadOnly(CType(Me.DataContext, ViewModels.ViewModelCmdEncoursRetrait).SelectedTrancheAge.NomFichier)
        End Sub

        Private Sub DataGridTrAge_AutoGeneratingColumn(ByVal sender As Object, ByVal e As System.Windows.Controls.DataGridAutoGeneratingColumnEventArgs) Handles DataGridTrAge.AutoGeneratingColumn

        End Sub

        Private Sub DataGridTrAge_BeginningEdit(ByVal sender As Object, ByVal e As System.Windows.Controls.DataGridBeginningEditEventArgs) Handles DataGridTrAge.BeginningEdit

        End Sub

        Private Sub DataGridTrAge_CellEditEnding(ByVal sender As Object, ByVal e As System.Windows.Controls.DataGridCellEditEndingEventArgs) Handles DataGridTrAge.CellEditEnding, XamDataGridTrCapitaux.CellEditEnding
            If DataGridTrAge.Columns.IndexOf(e.Column) = 2 Then
                Dim v As DataRowView = e.Row.Item

                Dim min = CInt((If(v(1).Equals(DBNull.Value), 0, v(1))))
                Dim max = CInt((If(v(2).Equals(DBNull.Value), 0, v(2))))


                Dim txt As TextBox = e.EditingElement

                Try
                    Dim editvalue As Integer = Convert.ToInt32(txt.Text.Trim())
                    If min < editvalue Then
                        v(2) = editvalue
                    Else
                        v(2) = min + 1
                        txt.Text = min + 1

                    End If
                Catch ex As Exception
                    If min < max Then
                        v(2) = max
                    Else
                        v(2) = min + 1
                        txt.Text = min + 1
                    End If
                End Try



                Dim q = From rv As DataRowView In v.DataView
                        Order By CInt((If(rv(1).Equals(DBNull.Value), 0, rv(1)))) Ascending
                        Select rv


                Dim execq = q.ToList()

                If execq.IndexOf(v) < q.Count - 1 Then
                    execq(execq.IndexOf(v) + 1)(1) = v(2)
                    For i As Integer = (execq.IndexOf(v) + 1) To q.Count - 1
                        If CInt(execq(i)(2)) < CInt(execq(i)(1)) Then
                            execq(i)(2) = CInt(execq(i)(1)) + 1
                            If i < q.Count - 1 Then
                                execq(i + 1)(1) = execq(i)(2)
                            End If
                        End If
                    Next
                End If
            End If
            'If DataGridTrAge.Columns.IndexOf(e.Column) = 1 Then
            '    e.Cancel = True
            'End If

        End Sub

        Private Sub DataGridTrAge_InitializingNewItem(ByVal sender As Object, ByVal e As System.Windows.Controls.InitializingNewItemEventArgs) Handles DataGridTrAge.InitializingNewItem, XamDataGridTrCapitaux.InitializingNewItem
            Dim v As DataRowView = e.NewItem

            Dim q = Aggregate rv As DataRowView In v.DataView
                    Into Max(CLng((If(rv(2).Equals(DBNull.Value), 0, rv(2)))))

            v(0) = "Tranche"
            v(1) = q
            v(2) = q + 5
        End Sub

        Private Sub DataGridTrAge_RowEditEnding(ByVal sender As Object, ByVal e As System.Windows.Controls.DataGridRowEditEndingEventArgs) Handles DataGridTrAge.RowEditEnding
            Dim v As DataRowView = e.Row.Item

            Dim min = CInt((If(v(1).Equals(DBNull.Value), 0, v(1))))
            Dim max = CInt((If(v(2).Equals(DBNull.Value), 0, v(2))))





            If min < max Then
                v(2) = max
            Else
                v(2) = min + 1
            End If

        End Sub


        Private Sub DataGridTrAge_UnloadingRow(ByVal sender As Object, ByVal e As System.Windows.Controls.DataGridRowEventArgs) Handles DataGridTrAge.UnloadingRow
            Try

                Dim v As DataRowView = TryCast(e.Row.Item, DataRowView)
                If v Is Nothing Then Return
                Dim index = v.Row.Table.Rows.IndexOf(v.Row)
                Dim table = v.Row.Table
                'Dim min = v.Row(1)
                'Dim max = v.Row(2)

                If index = table.Rows.Count - 1 Then
                    Return
                ElseIf index = 0 Then
                    table.Rows(index + 1)(1) = 0
                Else
                    table.Rows(index + 1)(1) = table.Rows(index - 1)(2)
                End If
            Catch ex As Exception

            End Try

        End Sub

        Private Sub DataGrid_LoadingRow(ByVal sender As Object, ByVal e As DataGridRowEventArgs) Handles DataGridTrAge.LoadingRow, XamDataGridTrCapitaux.LoadingRow
            'e.Row.Header = (e.Row.GetIndex + 1).ToString
        End Sub

    End Class
End Namespace
